<?php
/** class.simpleblog.php
 * 
 * Route user requests
 * 
 * @author Ted, tass2001-at-gmail-dot-com
 * @package phpsimpleblog
 * @version $Id: class.simpleblog.php 13 2013-03-31 02:20:53Z tass2001@gmail.com $
 * @website https://code.google.com/p/phpsimpleblog/
 */
class SimpleBlog{
    public function __construct(){
        $this->authen = new Authenticator();
        define('AUTH',$this->authen->checkCookie());
        $this->viewer = new Viewer();
        $this->editor = new Editor();
    }
    
    public function dispatch($URI){
        $clean_URI = $this->viewer->uriParse($URI,' ');
        switch($clean_URI[0]){
            case 'admin':
                $adm = new Admin();
                if(isset($clean_URI[1])){
                    switch($clean_URI[1]){
                        case 'comments':
                            echo $adm->commentManage($_REQUEST['approve'],$_REQUEST['ID']);
                            break;
                    }
                }else{
                    echo $adm->commentApprover();
                }
                break;
            case 'login':
                if(!(AUTH) && ($_REQUEST['username'] && $_REQUEST['password']))
                    echo $this->authen->checkCredentials($_REQUEST['username'],$_REQUEST['password']);
                else
                    echo $this->viewer->viewLogin();
                break;
            case 'post':
                if(is_numeric($clean_URI[1])){
                    echo $this->viewer->viewPost($clean_URI[1]);
                }else{
                    switch($clean_URI[1]){
                        case 'delete':
                            if(is_numeric($clean_URI[2]))
                                echo $this->editor->deletePost($clean_URI[2]);
                            break;
                        case 'edit':
                            echo $this->editor->editPost($clean_URI[2]);
                            break;
                        case 'hide':
                            if(is_numeric($clean_URI[2]))
                                echo $this->editor->hidePost($clean_URI[2]);
                            break;
                        case 'new':
                            echo $this->editor->newPost($_REQUEST);
                            break;
                        case 'comment':
                            echo $this->editor->newComment($_REQUEST,$clean_URI[2]);
                            break;
                        default:
                            echo $this->viewer->viewNotFound();
                            break;
                    }
                }
                break;
            case 'sitemap':
                header('Content-Type:text/xml');
                echo file_get_contents(DOC_ROOT.$this->viewer->config->static_content->sitemap);
                break;
            case 'special':
                switch($clean_URI[1]){
                    case 'delete':
                        echo $this->editor->deletePost($clean_URI[2],$is_special=true);
                        break;
                    case 'edit':
                        echo $this->editor->editPost($clean_URI[2]);
                        break;
                    default:
                        echo $this->viewer->viewSpecial($clean_URI[1]);
                        break;
                }
                break;
            case 'upload':
                echo $this->editor->upload();
                break;
            case '':
                echo $this->viewer->viewHome(0);
                break;
            default:
                echo $this->viewer->viewNotFound();
                break;
        }
    }
}
?>